﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UCL;
using UCL.Data;
using UCL.Data.ORM;

namespace DBKit5
{
    public partial class frmUDMToDatabase : Form
    {
        IDatabaseHelper idh;
        public frmUDMToDatabase()
        {
            InitializeComponent();
            fileBrowserField1.FileFilter = "Dll文件(*.dll)|*.dll";
            fileBrowserField1.FileName = Application.StartupPath + "\\UCL.Data.dll";
            tbNamespace.Text = "UCL.Data.ORM.UMD";
            ucConnectionBar1.ConnectionChangedHandler = oidh => { idh = oidh; };
        }

        private void frmUDMToDatabase_Load(object sender, EventArgs e)
        {
            ucConnectionBar1.uc_init();
            btnClickConfig();
        }
        private void btnClickConfig()
        {
            btnParse.Click += new EventHandler(btn_Click);
            btnRun.Click += new EventHandler(btn_Click);
        }
        private void btn_Click(object sender, EventArgs e)
        {
            if (fileBrowserField1.FileName.Length == 0)
            {
                MessageBox.Show("文件无效!");
                return;
            }
            switch (((Button)sender).Name)
            {
                case "btnParse":
                    tbSql.Text = idh == null ? EntityHelper.GetCreateTableTextInNameSpace(fileBrowserField1.FileName, tbNamespace.Text, "System.Data.SqlClient", null) : EntityHelper.GetUpdateTableTextByDll(fileBrowserField1.FileName, idh.ConnectionString, idh.ProviderName, tbNamespace.Text, null);
                    break;
                case "btnRun":
                    if (idh == null)
                    {
                        MessageBox.Show("连接对象无效!");
                        return;
                    }
                    if (tbSql.TextLength == 0) return;
                    idh.ExecuteNonQuery(tbSql.Text);
                    MessageBox.Show(idh.MessageSet.Count == 0 ? "执行成功!" : idh.MessageSet.ToString(Environment.NewLine));
                    break;
            }
        }
    }
}
